package arithmetic3.bitmap;

/**
 * @author 姚凤桥
 * @Date 2021/12/15 10:31
 * @Description: 位图
 */
public class BitMap {
    private int[] bitMap;

    private int size;

    /**
     * 使用指定的整型数组来充当位图
     *
     * @param bitMap
     */
    public BitMap(int[] bitMap) {
        this.bitMap = bitMap;
        this.size = bitMap.length << 5;
    }

    /**
     * 获取位图中某一位的状态
     *
     * @return
     */
    public int getIndexStatus(int index) {
        int intIndex = index >> 5;
        int digit = index % 32;
        return (bitMap[intIndex] >> digit) & 1;
    }

    /**
     * 将位图中某一位的状态修改为1
     *
     * @param index
     */
    public void modifyIndexOne(int index) {
        int intIndex = index >> 5;
        int digit = index % 32;
        bitMap[intIndex] = bitMap[intIndex] | (1 << digit);
    }

    /**
     * 将位图中某一位状态修改为0
     *
     * @param index
     */
    public void modifyIndexZero(int index) {
        int intIndex = index >> 5;
        int digit = index % 32;
        bitMap[intIndex] = bitMap[intIndex] & ~(1 << digit);
    }
}
